home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-04-13 | 7.9 KB | 266 lines | [TEXT/ALFA] |
- /* Three-way file comparison program (diff3) for Project GNU
- Copyright (C) 1988, 1989 Free Software Foundation, Inc.
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 1, or (at your option)
- any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
-
-
- /* Written by Randy Smith */
-
- #undef EXTERN
- #ifndef GDIFF3_MAIN
- #define EXTERN extern
- #else
- #define EXTERN
- #endif
-
- /*
- * Internal data structures and macros for the diff3 program; includes
- * data structures for both diff3 diffs and normal diffs.
- */
-
- /*
- * Different files within a diff
- */
- #define FILE0 0
- #define FILE1 1
- #define FILE2 2
-
- /*
- * Three way diffs are build out of two two-way diffs; the file which
- * the two two-way diffs share is:
- */
- #define FILEC FILE0
-
- /* The ranges are indexed by */
- #define START 0
- #define END 1
-
- enum diff_type {
- ERROR, /* Should not be used */
- ADD, /* Two way diff add */
- CHANGE, /* Two way diff change */
- DELETE, /* Two way diff delete */
- DIFF_ALL, /* All three are different */
- DIFF_1ST, /* Only the first is different */
- DIFF_2ND, /* Only the second */
- DIFF_3RD /* Only the third */
- };
-
- #ifdef DIRECT_DIFF
- /* Two-way diff */
- /*
- * When "diff" is included with this project, the "diff_block"
- * structure is exactly the same as a "change".
- */
- #define diff_block change
- #define next link
-
- #else DIRECT_DIFF
- /* Two-way diff */
- struct diff_block {
- LONG ranges[2][2]; /* Ranges are inclusive */
- char **lines[2]; /* The actual lines (may contain nulls) */
- LONG *lengths[2]; /* Line lengths (including newlines, if any) */
- struct diff_block *link;
- };
- #endif
-
- /* Three-way diff.
- * Note that when the "diff" is included in this project, (struct diff3_block *)
- * can be typecasted to (struct change *) safely!
- * If you change this structure, also make sure you change "struct change".
- */
-
- struct diff3_block {
- struct diff3_block *link;
- char correspond; /* Type of diff. Really, the type is enum diff_type. */
- LONG ranges[3][2]; /* Ranges are inclusive */
- #ifndef DIRECT_DIFF
- char **lines[3]; /* The actual lines (may contain nulls) */
- LONG *lengths[3]; /* Line lengths (including newlines, if any) */
- #endif
- };
-
- /*
- * Access the ranges on a diff block.
- */
- #define D_LOWLINE(diff, filenum) \
- ((diff)->ranges[filenum][START])
- #define D_HIGHLINE(diff, filenum) \
- ((diff)->ranges[filenum][END])
- #define D_NUMLINES(diff, filenum) \
- (D_HIGHLINE((diff), (filenum)) - D_LOWLINE((diff), (filenum)) + 1)
-
- /*
- * Access the line numbers in a file in a diff by relative line
- * numbers (i.e. line number within the diff itself). Note that these
- * are lvalues and can be used for assignment.
- *
- * However, when "diff" is included in the project, these
- * are NOT lvalues! This is because changing them would be
- * changing the actual file buffers!
- */
- #ifndef DIRECT_DIFF
-
- #define D_RELNUM(diff, filenum, linenum) \
- (*((diff)->lines[filenum] + linenum))
- #define D_RELLEN(diff, filenum, linenum) \
- (*((diff)->lengths[filenum] + linenum))
-
- #else DIRECT_DIFF
-
- #define D_RELNUM(diff, filenum, linenum) \
- (diff3_file_data[filenum].linbuf[(linenum) + D_LOWLINE((diff), (filenum))].text + 0)
- #define D_RELLEN(diff, filenum, linenum) \
- (diff3_file_data[filenum].linbuf[(linenum) + D_LOWLINE((diff), (filenum))].length + 0)
-
- #endif DIRECT_DIFF
-
- /*
- * And get at them directly, when that should be necessary.
- */
- #define D_LINEARRAY(diff, filenum) \
- ((diff)->lines[filenum])
- #define D_LENARRAY(diff, filenum) \
- ((diff)->lengths[filenum])
-
- /*
- * Next block.
- */
- #define D_NEXT(diff) ((diff)->next)
-
- /*
- * Access the type of a diff3 block.
- */
- #define D3_TYPE(diff) ((diff)->correspond)
-
- /*
- * Line mappings based on diffs. The first maps off the top of the
- * diff, the second off of the bottom.
- */
- #define D_HIGH_MAPLINE(diff, fromfile, tofile, lineno) \
- ((lineno) \
- - D_HIGHLINE ((diff), (fromfile)) \
- + D_HIGHLINE ((diff), (tofile)))
-
- #define D_LOW_MAPLINE(diff, fromfile, tofile, lineno) \
- ((lineno) \
- - D_LOWLINE ((diff), (fromfile)) \
- + D_LOWLINE ((diff), (tofile)))
-
- /*
- * General memory allocation function.
- */
- #define ALLOCATE(number, type) \
- (type *) xmalloc ((number) * sizeof (type))
-
- /*
- * Options variables for flags set on command line.
- *
- * ALWAYS_TEXT: Treat all files as text files; never treat as binary.
- *
- * EDSCRIPT: Write out an ed script instead of the standard diff3 format.
- *
- * FLAGGING: Indicates that in the case of overlapping diffs (type
- * DIFF_ALL), the lines which would normally be deleted from file 1
- * should be preserved with a special flagging mechanism.
- *
- * DONT_WRITE_OVERLAP: 1 if information for overlapping diffs should
- * not be output.
- *
- * DONT_WRITE_SIMPLE: 1 if information for non-overlapping diffs
- * should not be output.
- *
- * FINALWRITE: 1 if a :wq should be included at the end of the script
- * to write out the file being edited.
- *
- * MERGE: output a merged file.
- */
- EXTERN int always_text;
- EXTERN int edscript;
- EXTERN int flagging;
- EXTERN int dont_write_overlap;
- EXTERN int dont_write_simple;
- EXTERN int finalwrite;
- EXTERN int merge;
-
- extern int optind;
-
- EXTERN char *argv0;
-
- #ifdef DIRECT_DIFF
- /*
- * We need this so we can access the buffers for the original files. */
- EXTERN struct file_data diff3_file_data[3];
- #endif
-
- /*
- * Forward function declarations. (prototypes)
- */
- #ifdef __STDC__
- struct diff_block *process_diff (struct file_data *filea, struct file_data *fileb);
- struct diff3_block *make_3way_diff (struct diff_block *thread1, struct diff_block *thread2);
- void output_diff3 ( FILE *outputfile, struct diff3_block *diff, int mapping[3], int rev_mapping[3] );
- int output_diff3_edscript ( FILE *outputfile, struct diff3_block *diff, int mapping[3], int rev_mapping[3], char *file0, char *file1, char *file2);
- int output_diff3_merge ();
- void usage (void);
-
- struct diff3_block *using_to_diff3_block (struct diff_block *using[2], struct diff_block *last_using[2], int low_thread, int high_thread, struct diff3_block *last_diff);
- int copy_stringlist (char *fromptrs[], LONG *fromlengths, char *toptrs[], LONG *tolengths, LONG copynum);
- struct diff3_block *create_diff3_block (LONG low0, LONG high0, LONG low1, LONG high1, LONG low2, LONG high2);
- #ifndef DIRECT_DIFF
- int compare_line_list ( char *list1[], LONG *lengths1, char *list2[], LONG *lengths2, LONG nl);
- #else
- int compare_line_list ( struct line_def list1[], struct line_def list2[], LONG nl);
- #endif DIRECT_DIFF
-
- char *read_diff (char *filea, char *fileb, char **output_placement);
- enum diff_type process_diff_control ( char **string, struct diff_block *db);
- char *scan_diff_line ( char *scan_ptr, char **set_start, LONG *set_length, char *limit, char firstchar);
-
- struct diff3_block *reverse_diff3_blocklist (struct diff3_block *diff);
-
- LONG myread (int fd, char *ptr, LONG size);
- int perror_with_exit (char *string);
-
- int output_diff3_merge (FILE *commonfile, FILE *outputfile, struct diff3_block *diff, int mapping[3], int rev_mapping[3],
- char *file0, char *file1, char *file2);
- #else __STDC__
- /*
- * Forward function declarations.
- */
- struct diff_block *process_diff ();
- struct diff3_block *make_3way_diff ();
- void output_diff3 ();
- int output_diff3_edscript ();
- int output_diff3_merge ();
- void usage ();
-
- struct diff3_block *using_to_diff3_block ();
- int copy_stringlist ();
- struct diff3_block *create_diff3_block ();
- int compare_line_list ();
-
- char *read_diff ();
- enum diff_type process_diff_control ();
- char *scan_diff_line ();
-
- struct diff3_block *reverse_diff3_blocklist ();
-
- VOID *xmalloc ();
- VOID *xrealloc ();
-
- #endif
-